package com.qzsoft.zhjd.machine.modules.machineinfo.service.impl;

import java.io.Serializable;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qzsoft.zhjd.machine.modules.machineinfo.mapper.MachineInfoDriverMapper;
import com.qzsoft.zhjd.common.components.BaseServiceComponent;
import com.qzsoft.zhjd.common.enums.BizCode;
import com.qzsoft.zhjd.common.utils.RenExceptionUtils;
import com.qzsoft.zhjd.machine.modules.machineinfo.dto.MachineInfoDriverDto;
import com.qzsoft.zhjd.machine.modules.machineinfo.entity.MachineInfoDriverEntity;
import com.qzsoft.zhjd.machine.modules.machineinfo.service.MachineInfoDriverService;

/**
 * @ClassName MachineInfoDriverServiceImpl
 * @Description 机械设备驾驶员业务接口实现
 * @author linhuawei
 * @since 2021-11-01
 */
@Service
public class MachineInfoDriverServiceImpl extends ServiceImpl<MachineInfoDriverMapper, MachineInfoDriverEntity>
        implements MachineInfoDriverService {

    @Autowired
    private BaseServiceComponent baseServiceComponent;

    @Override
    public IPage<MachineInfoDriverDto> pageDto(Map<String, Object> params) {
        IPage<MachineInfoDriverDto> page = baseServiceComponent.getPage(params, MachineInfoDriverDto.class);
        return baseMapper.queryDto(page, params);
    }

    @Override
    public boolean save(MachineInfoDriverEntity entity) {
        // 判断驾驶员是否已经存在
        QueryWrapper<MachineInfoDriverEntity> queryWrapper = new QueryWrapper<MachineInfoDriverEntity>();
        queryWrapper.eq("driver_id", entity.getDriverId());
        queryWrapper.eq("machine_id", entity.getMachineId());
        Integer count = baseMapper.selectCount(queryWrapper);
        if (null != count && 0 < count) {
            RenExceptionUtils.throwEx(BizCode.PARAMS_ERROR, "当前机械设备已经绑定改操作员");
        }

        // 执行添加操作
        return super.save(entity);
    }

    @Override
    public void deleteByMachineId(Serializable machineId) {
        QueryWrapper<MachineInfoDriverEntity> queryWrapper = new QueryWrapper<MachineInfoDriverEntity>();
        queryWrapper.eq("machine_id", machineId);
        baseMapper.delete(queryWrapper);
    }

}
